home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib43 / mntlib / select.c < prev    next >
C/C++ Source or Header  |  1993-11-02  |  1KB  |  62 lines

  1. /*
  2.  * select() emulation for MiNT. Written by Eric R. Smith and placed in the
  3.  * public domain
  4.  */
  5.  
  6. #include <errno.h>
  7. #include <mintbind.h>
  8. #include <types.h>
  9. #include <time.h>
  10.  
  11. int
  12. select(junk, rfds, wfds, xfds, timeout)
  13.     int junk;
  14.     fd_set *rfds, *wfds, *xfds;
  15.     struct timeval *timeout;
  16. {
  17. /* the only tricky part, really, is figuring out the timeout value.
  18.    a null pointer means indefinite timeout, which is the same as a 0
  19.    value under MiNT. A non-null pointer to a 0 valued struct means
  20.    to poll; in MiNT we simulate this with a minimum timeout value.
  21.  */
  22.     unsigned long mtime;
  23.     unsigned short stime;
  24.     int rval;
  25.     fd_set save_rfds = 0, save_wfds = 0, save_xfds = 0;
  26.  
  27.     if (rfds) save_rfds = *rfds;
  28.     if (wfds) save_wfds = *wfds;
  29.     if (xfds) save_xfds = *xfds;
  30.     if (timeout) {
  31.         mtime = timeout->tv_sec * 1000 + timeout->tv_usec/1000;
  32.         if (mtime == 0) mtime = 1;
  33.     }
  34.     else
  35.         mtime = 0;
  36.  
  37.     /* Unfortunately, Fselect can only handle at most 65535ms timeout.
  38.        We have to loop for a bigger timeout. */
  39.     for (;;)
  40.       {
  41.         if (mtime > 65535U)
  42.           stime = 65535U;
  43.         else
  44.           stime = (unsigned short)mtime;
  45.         mtime -= stime;
  46.         rval = Fselect (stime, (long *) rfds, (long *) wfds, (long *) xfds);
  47.         if (rval < 0)
  48.           {
  49.         errno = -rval;
  50.         return -1;
  51.           }
  52.         if (rval == 0 && mtime > 0)
  53.           {
  54.         if (rfds) *rfds = save_rfds;
  55.         if (wfds) *wfds = save_wfds;
  56.         if (xfds) *xfds = save_xfds;
  57.           }
  58.         else
  59.           return rval;
  60.       }
  61. }
  62.